<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools>
        <template #left>权限设置</template>
        <el-button size="small" icon="el-icon-edit" type="primary" @click="add(1, '0')">添加一级权限</el-button>
      </PageTools>
      <el-card v-loading="loading">
        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="菜单名称" prop="name" />
          <el-table-column label="权限标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="add(2, row.id)"
              >添加权限点</el-button>
              <el-button size="small" type="text" @click="edit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 权限新增/编辑的弹层 -->
      <el-dialog :visible.sync="showDialog" width="40%" :title="title" @closed="closed">
        <!-- 表单内容 -->
        <el-form ref="form" label-width="100px" :model="form" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入权限名称" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="form.code" placeholder="请输入权限标识" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="form.description" placeholder="请输入权限的描述" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="form.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="addPermission">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addPermission, delPermission, getPermissionDetail, getPermissionList, updatePermission } from '@/api/permisson'
import { listToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      showDialog: false,
      loading: false,
      // 基于接口文档准备数据
      form: {
        enVisible: '1', // 1开启, 0关闭
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空' }],
        code: [{ required: true, message: '权限标识不能为空' }]
      }
    }
  },
  computed: {
    title() {
      return this.form.id ? '修改权限' : '添加权限'
    }
  },
  created() {
    this.getPermissionList() // 获取所有权限点
  },
  methods: {
    async getPermissionList() {
      this.loading = true
      const data = await getPermissionList()
      this.permissionList = listToTree(data, '0')
      this.loading = false
    },
    // 删除权限
    async del(id) {
      try {
        await this.$confirm('此操作将删除该权限, 是否继续?', '提示', { type: 'warning' })
      } catch {
        return false
      }
      await delPermission(id)
      this.$message.success('删除权限成功')
      this.getPermissionList() // 重新渲染
    },
    // 关闭弹窗之后
    closed() {
      // 重置校验
      this.$refs.form.resetFields()
      this.form = {
        enVisible: '1', // 1开启, 0关闭
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 显示弹框
    add(type, pid) {
      this.form.type = type
      this.form.pid = pid
      this.showDialog = true
    },
    // 编辑权限按钮
    async edit(id) {
      this.form = await getPermissionDetail(id)
      this.showDialog = true
    },
    async addPermission() {
      try {
        await this.$refs.form.validate()
      } catch {
        return false
      }
      if (this.form.id) {
        await updatePermission(this.form)
        this.$message.success('修改权限成功')
      } else {
        await addPermission(this.form)
        this.$message.success('添加权限成功')
      }
      this.showDialog = false
      this.getPermissionList() // 重新渲染
    }
  }
}
</script>

<style>

</style>
